#!/usr/bin/env bash
# db-assign_sequential_id -- Assigns a unique integer to every row for a table using PostgreSQL sequence generator
# > eval "$(db-parse "$url")"
# > db-assign_sequential_id TABLE ID_COLUMN [PART_ID_COLUMN]
##
set -euo pipefail

[[ $# -gt 0 ]] || usage "$0" "Missing TABLE_CSV"
[[ $# -gt 1 ]] || usage "$0" "Missing ID_COLUMN"

TABLE_CSV=$1 IdColumn=$2 PartColumn=${3:-0}

db-supports_pg_lang "plpythonu"

SQL="
    CREATE OR REPLACE FUNCTION seqassign(partid INT, reset BOOL)
    RETURNS BIGINT AS \$\$
      if reset:
        for pid in range(partid):
          SD[pid] = 0
        return 0

      x = SD[partid]
      SD[partid] += 1
      return x + (partid << 48)
    \$\$ LANGUAGE plpythonu;

    SELECT seqassign(1 << 16, true);
"

for table in $(echo $TABLE_CSV | sed "s/,/ /g")
do
    SQL="$SQL
    UPDATE $table SET $IdColumn = seqassign($PartColumn, false);"
done

db-execute "$SQL"
